package com.yic.module.qms.controller.admin.inspectinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.qms.controller.admin.inspectinfo.vo.*;
import com.yic.module.qms.dal.dataobject.inspectinfo.InspectInfoDO;
import com.yic.module.qms.convert.inspectinfo.InspectInfoConvert;
import com.yic.module.qms.service.inspectinfo.InspectInfoService;

@Tag(name = "管理后台 - 检验单身")
@RestController
@RequestMapping("/qms/inspect-info")
@Validated
public class InspectInfoController {

    @Resource
    private InspectInfoService inspectInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建检验单身")
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:create')")
    public CommonResult<Long> createInspectInfo(@Valid @RequestBody InspectInfoCreateReqVO createReqVO) {
        return success(inspectInfoService.createInspectInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检验单身")
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:update')")
    public CommonResult<Boolean> updateInspectInfo(@Valid @RequestBody InspectInfoUpdateReqVO updateReqVO) {
        inspectInfoService.updateInspectInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检验单身")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:delete')")
    public CommonResult<Boolean> deleteInspectInfo(@RequestParam("id") Long id) {
        inspectInfoService.deleteInspectInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检验单身")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:query')")
    public CommonResult<InspectInfoRespVO> getInspectInfo(@RequestParam("id") Long id) {
        InspectInfoDO inspectInfo = inspectInfoService.getInspectInfo(id);
        return success(InspectInfoConvert.INSTANCE.convert(inspectInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得检验单身列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:query')")
    public CommonResult<List<InspectInfoRespVO>> getInspectInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectInfoDO> list = inspectInfoService.getInspectInfoList(ids);
        return success(InspectInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检验单身分页")
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:query')")
    public CommonResult<PageResult<InspectInfoRespVO>> getInspectInfoPage(@Valid InspectInfoPageReqVO pageVO) {
        PageResult<InspectInfoDO> pageResult = inspectInfoService.getInspectInfoPage(pageVO);
        return success(InspectInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检验单身 Excel")
    @PreAuthorize("@ss.hasPermission('qms:inspect-info:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectInfoExcel(@Valid InspectInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectInfoDO> list = inspectInfoService.getInspectInfoList(exportReqVO);
        // 导出 Excel
        List<InspectInfoExcelVO> datas = InspectInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "检验单身.xls", "数据", InspectInfoExcelVO.class, datas);
    }

}
